<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Priv extends MY_Controller {

	function index() {
		header("Location: /priv/role");
	}

	function role() {
		$this->load->view('priv/role', array('data' => $this->data));
	}	

	function role_create() {
		$current_user = $this->session->userdata('user_info');
		$data['operator'] = $current_user['id'];
		$data['role'] = $this->input->post('role', TRUE);
		$data['remark'] = $this->input->post('remark', TRUE);

		$this->load->model('role');
		$r = $this->role->rfilter($data);
		if ($r['status']) {
			$this->role->create($data);
			echo json_encode(array('r' => 'succ', 'msg' => $r['msg']));
		} else {
			echo json_encode(array('r' => 'fail', 'msg' => $r['msg']));
		}
		
	}

	function rlist($id=0) {
		$this->load->model('role');
		$id = intval($id);
		$per_page = 10;
		$this->data['role'] = $this->role->all_role($id, $per_page);

		$config['base_url'] = '/priv/rlist/';
		$config['total_rows'] = $this->role->rtotal();
		$config['per_page'] = $per_page; 

		$this->load->library('pagination');
		$this->pagination->initialize($config); 
		$this->load->view('priv/rlist', array('data' => $this->data));
	}

	function rlist_delete($id = 0) {
		if (intval($id) == 0) {
			echo json_encode(array('r' => 'fail', 'msg' => '角色ID为空'));
			return;
		}
		$this->load->model('role');
		$this->role->del(intval($id));
		echo json_encode(array('r' => 'succ', 'msg' => '操作成功'));
	}

	function grant() {
		$this->load->model('cdn_user');
		$this->load->model('role');
		$this->data['all_user'] = $this->cdn_user->all_user(0, 100000);
		$this->data['all_group'] = $this->cdn_user->all_group(0, 100000);
		$this->data['all_role'] = $this->role->all_role(0, 100000);
		$this->load->view('priv/grant', array('data' => $this->data));
	}

	function redit($id = 0) {
		$flag = TRUE;
		if (intval($id) == 0) $flag = FALSE;
		
		$this->load->model('role');
		$role = $this->role->find($id);
		if (!$role) {
			$flag = FALSE;
		} else {
			$this->data['role'] = $role[0];
			$this->data['no_res'] = $this->role->fetch_priv($role[0]['id'], 0);
			$this->data['person_res'] = $this->role->fetch_priv($role[0]['id'], 1);
			$this->data['all_res'] = $this->role->fetch_priv($role[0]['id'], 2);
		}

		$this->data['flag'] = $flag;
		$this->data['module_tree'] = $this->role->module_tree();
		$this->data['all_role'] = $this->role->all_role(0, 100000);
		$this->load->view('priv/redit', array('data' => $this->data));
	}

	function redit_info() {
		$current_user = $this->session->userdata('user_info');
		$data['operator'] = $current_user['id'];
		$data['id'] = $this->input->post('role_id', TRUE);
		$data['role'] = $this->input->post('role', TRUE);
		$data['remark'] = $this->input->post('remark', TRUE);
		$this->load->model('role');
		$r = $this->role->rufilter($data);
		if ($r['status']) {
			$this->role->up($data['id'], $data);
			echo json_encode(array('r' => 'succ', 'msg' => $r['msg']));
		} else {
			echo json_encode(array('r' => 'fail', 'msg' => $r['msg']));
		}
	}

	function redit_priv() {
		$current_user = $this->session->userdata('user_info');
		$data['operator'] = $current_user['id'];
		$priv = $this->input->post('priv', TRUE);
		$tmp = explode('_', strval($priv));
		$data['role_id'] = intval($tmp[0]);
		$data['module_id'] = intval($tmp[1]);
		$data['node_id'] = intval($tmp[2]);
		$action = $tmp[3];
		$this->load->model('role');
		if ($action == 'a') {
			$r = $this->role->priv_filter($data);
			if ($r['status']) {
				$this->role->add_priv($data);
				echo json_encode(array('r' => 'succ', 'msg' => $r['msg']));
			} else {
				echo json_encode(array('r' => 'fail', 'msg' => $r['msg']));
			}
		} 
		if ($action == 'd') {
			$this->role->del_priv($data);
			echo json_encode(array('r' => 'succ', 'msg' => $r['msg']));
		}
	}
}
